<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="检查ID" prop="studyId">
        <el-input v-model="queryParams.studyId" placeholder="请输入检查ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="病人ID" prop="patientId">
        <el-input v-model="queryParams.patientId" placeholder="请输入病人ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="检查类型" prop="studyType">
        <el-input v-model="queryParams.studyType" placeholder="请输入检查类型" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="检查项目" prop="studyItem">
        <el-input v-model="queryParams.studyItem" placeholder="请输入检查项目" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="检查部位" prop="studyPart">
        <el-input v-model="queryParams.studyPart" placeholder="请输入检查部位" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="检查号" prop="studyNum">
        <el-input v-model="queryParams.studyNum" placeholder="请输入检查号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="门诊号" prop="outpatientNum">
        <el-input v-model="queryParams.outpatientNum" placeholder="请输入门诊号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Upload" @click="handleUpload"
          v-hasPermi="['pacs:dicom:upload']">上传</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="检查ID" align="center" prop="studyId" :show-overflow-tooltip="true" />
      <el-table-column label="病人ID" align="center" prop="patientId" />
      <el-table-column label="检查类型" align="center" prop="studyType" />
      <el-table-column label="检查项目" align="center" prop="studyItem" />
      <el-table-column label="检查部位" align="center" prop="studyPart" />
      <el-table-column label="检查号" align="center" prop="studyNum" />
      <el-table-column label="门诊号" align="center" prop="outpatientNum" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="200" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleInfo(scope.row)" v-hasPermi="['pacs:dicom:info','pacs:serise:alllist']">详情</el-button>
          <el-button link type="warning" @click="handleShowDetail(scope.row)"> 显示图像详情</el-button>
          <el-button link type="warning" @click="handleShowDetailV1(scope.row)"> 显示图像详情V1</el-button>
          <el-button link type="danger" @click="handleShowDetailV2(scope.row)"> 显示图像详情V2</el-button>
          <el-button link type="danger" @click="handleShowDetailV3(scope.row)"> 显示图像详情V3</el-button>
          <el-button link type="danger" @click="handleShowDetailV4(scope.row)">显示图像详情V4</el-button>
          <el-button link type="danger" @click="handleShowDetail5(scope.row)">显示图像详情V5</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 上传医学影像 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="uploadRef" label-width="80px">
        <el-form-item>
          <DicomUpload ref="uploaderRef" :limit="50" :file-size="100" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :loading="uploading" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- DICOM查看器对话框 -->
    <!-- <DicomViewerPage v-model="showViewer" :initial-study-id="studyId" :initial-study-uid="studyUid"/> -->
    <!-- <dicom-detail v-model="showDetail" :initial-study-id="studyId" :initial-study-uid="studyUid"></dicom-detail> -->
  </div>
</template>

<script setup name="Study">

import { useRouter } from 'vue-router'


const router = useRouter();
import { listStudy, getStudy, delStudy, addStudy, updateStudy } from "@/api/pacs/study/study"
import DicomUpload from "./components/DicomUpload"
// import DicomViewerPage from '../dicom/DicomViewerPage'
// import DicomDetail from '../dicom/DicomDetail'


const { proxy } = getCurrentInstance()

const studyList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const dicomFiles = ref('')
const uploaderRef = ref()
const uploading = ref(false)


const showViewer = ref(false) //显示详情1

const showDetail=ref(false) //显示详情对话框2

const studyId = ref(null) 
const studyUid = ref('')

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    studyId: null,
    patientId: null,
    studyType: null,
    studyItem: null,
    studyPart: null,
    studyNum: null,
    outpatientNum: null,
  },
})

const { queryParams } = toRefs(data)

/** 查询检查实体类列表 */
function getList() {
  loading.value = true
  listStudy(queryParams.value).then(response => {
    studyList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  uploaderRef.value.clearFiles()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 上传按钮操作 */
function handleUpload() {
  open.value = true
  title.value = "上传医学影像"
}

/** 详情按钮操作 */
function handleInfo(row) {
  studyId.value = row.id
  studyUid.value = row.studyId
  showViewer.value = true
}
/***
 * 显示详情图像
 */
function handleShowDetailV1(row){
  studyId.value = row.id;
  studyUid.value = row.studyId;


  router.push({ 
    path: '/pacs/detailv1',
    query:{studyId:studyId.value,studyUid:studyUid.value} 
  });

}

function handleShowDetailV2(row){
  studyId.value = row.id;
  studyUid.value = row.studyId;


  router.push({ 
    path: '/pacs/detailv2',
    query:{studyId:studyId.value,studyUid:studyUid.value} 
  });

}

function handleShowDetailV3(row){

  router.push({ 
    path: '/pacs/detailv3',
  });

}

function handleShowDetailV4(row){

  router.push({ 
    path: '/pacs/detailv4',
  });

}

function handleShowDetail5(row){
  studyId.value = row.id;
  studyUid.value = row.studyId;

  router.push({
    path: '/pacs/detailv5',
    query: { studyId: studyId.value, studyUid: studyUid.value }
  });
}

function handleShowDetail(row){
  studyId.value = row.id;
  studyUid.value = row.studyId;
  console.log(studyUid.value);

  router.push({ 
    path: '/pacs/detail',
    query:{studyId:studyId.value,studyUid:studyUid.value} 
  });

}



/** 详情关闭操作 */
function handleInfoClose() {
  console.log('DICOM查看器已关闭')
}

/** 提交按钮 */
async function submitForm() {
  const fileCount = uploaderRef.value.getFileCount()

  if (fileCount === 0) {
    alert('请先选择要上传的文件', '提示', {
      type: 'warning'
    })
    return
  }

  const totalSize = uploaderRef.value.getTotalFileSize()

  try {
    uploading.value = true
    await uploaderRef.value.startUpload()
    return
  } catch (error) {
    // 用户取消上传或上传失败
    console.log('上传取消或失败:', error)
  } finally {
    uploading.value = false
    cancel();
    handleQuery();
  }
}

getList()
</script>